package main;

import character.Enemy;
import character.Player;
import config.Config;
import managers.InputManager;
import managers.ScreenManager;
import sprite.Animator;
import utils.FpsHandler;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;

import world.Background;
import world.RessourceLoader;

public class GameApp {
	private boolean _gameRunning = true;
	private Frame _frm;
	private Font _font;
	private FpsHandler _fpsh;
	private static RessourceLoader _resLoader = new RessourceLoader();
	private Background background;

	private Enemy _enemy;
	private Player _player;

	public GameApp(){
		try{

			background = new Background();
			_resLoader.loadAnimations();

			Animator animator = new Animator(Config.PLAYER_JASMIN);
			_player = new Player(animator,60,44,18,98
					,65,60,50,11);
			_player.getTransform().setOrigin(72, 144);
//			_player.getTransform().setOrigin(72, 0);
			_player.move(60,220);

//			_player.getTransform().scale(0.1,0.1);
			_player.getTransform().scale(2,2);

			_player.setLockedBounds(Config.BOUND_TOP, Config.BOUND_BOTTOM,
					Config.BOUND_LEFT, Config.BOUND_RIGHT);


			Animator animatorEnemy = new Animator("enemy3");
			_enemy = new Enemy(_player, animatorEnemy,88, 38, 18, 88, // HurtBox
					93, 54, 50, 11);


			_enemy.getTransform().setOrigin(97, 110);
//			_enemy.getTransform().setOrigin(97, 144);
			_enemy.move(30,220);
//			_enemy.getTransform().scale(0.1,0.1);
			_enemy.getTransform().scale(2,2);

			_font = Font.createFont(Font.TRUETYPE_FONT, getClass().getClassLoader().getResourceAsStream("res/simsun.ttf"));
			_font = _font.deriveFont(18f);

			_fpsh = new FpsHandler();

			_frm = new Frame();
			_frm.setUndecorated(true);
			_frm.setIgnoreRepaint(true);
			_frm.setTitle("");
			_frm.addKeyListener(InputManager.getInstance());

			JButton button = new JButton("close");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					_gameRunning = false;
					_frm.dispose();
				}
			});
			_frm.add(button);

//			_frm.setSize(640,480);
//			_frm.setLocation(100, 100);
//			_frm.setVisible(true);
//			_frm.setResizable(false);

			new ScreenManager(_frm);

	        _gameLoop();

		}catch (Exception e) {
            e.printStackTrace();
		}finally{
			ScreenManager.device.setFullScreenWindow(null);
			System.out.println("Fullscreen Exit");
			System.exit(0);
		}
	}
	
	private void _gameLoop(){
		BufferStrategy buff = _frm.getBufferStrategy();
		long lastTime = System.nanoTime();
		long optimalTime = 1000000000 / Config.FPS;

		while(_gameRunning){
			Graphics2D g = (Graphics2D)buff.getDrawGraphics();
			long now = System.nanoTime();
			long updateTime = now - lastTime;
			lastTime = now;


			// Rendering
			_initRendering(g);

			g.setFont(_font);

			if(Config.DEBUG){
				_displayInfoText(g, updateTime);
			}


			background.update(g);

			g.setColor(Color.red);
			g.draw(_player.getHurtBox());
			g.draw(_player.getHitBox());

			g.draw(_enemy.getHurtBox());
			g.draw(_enemy.getHitBox());

			_enemy.update(g);
			_player.update(g);


			g.dispose();

			if (!buff.contentsLost())
		    {
				buff.show();
		    }

			InputManager.getInstance().update();
			Toolkit.getDefaultToolkit().sync();

		    try {
				long duringTimeHao = (System.nanoTime()-lastTime)/1000000;
				long optimalTimeHao = optimalTime / 1000000;
				if(duringTimeHao >= optimalTimeHao){
					Thread.sleep(0);
				}
				else{
					Thread.sleep(optimalTimeHao - duringTimeHao);
				}


            } catch (InterruptedException e) {
                e.printStackTrace();
            }
		}
	}

	private void _initRendering(Graphics2D g){
		g.setColor(Color.black);
		g.fillRect(0, 0, 800, 600);
	}

	private void _displayInfoText(Graphics2D g, long updateTime){
		g.setColor(Color.white);
		g.drawString(Config.TITEL+ " "+ Config.VERSION, 20, 20);
		int fps = _fpsh.getFps(updateTime);
		g.drawString("FPS: "+fps, 20, 35);
	}

}
